package test03;
class MyCircularQueue {
    private int[] data;
    private int front;
    private int rear;

    public MyCircularQueue(int k) {
        data = new int[k+1];
        front = 0;
        rear = 0;
    }

    public boolean enQueue(int value) {
        if(isFull()) {
            return false;
        }
        data[rear] = value;
        rear = getNxt(rear);
        return true;
    }

    public boolean deQueue() {
        if(isEmpty()) {
            return false;
        }
        front = getNxt(front);
        return true;
    }

    public int Front() {
        if(isEmpty()) {
            return -1;
        }
        return data[front];
    }

    public int Rear() {
        if(isEmpty()) {
            return -1;
        }
        return data[(rear - 1 + data.length) % data.length];
    }

    public boolean isEmpty() {
        return rear == front;
    }

    public boolean isFull() {
        return getNxt(rear) == front ? true : false;
    }

    private int getNxt(int cur) {
        return (cur + 1) % data.length;
    }
}




